package com.gxuwz.handle;

import com.gxuwz.entity.WebResult;
import com.gxuwz.enums.WebResultEnum;
import com.gxuwz.exception.MyException;
import com.gxuwz.util.WebResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by h on 2017/12/4.
 */
@ControllerAdvice
public class ExceptionHandle {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public WebResult handle(Exception e){
        if(e instanceof MyException){
            MyException myException = (MyException)e;
            return WebResultUtil.error(myException.getCode(),myException.getMessage());
        }else {
            logger.error("【系统异常】{}",e);
            return WebResultUtil.error(WebResultEnum.UNKONW_ERROR.getCode(), WebResultEnum.UNKONW_ERROR.getMessage());
        }
    }
}
